package com.seventeen.mq.proxy.core.service.impl;

import com.seventeen.mq.proxy.common.constant.DelFlagConstants;
import com.seventeen.mq.proxy.common.exception.base.BaseException;
import com.seventeen.mq.proxy.core.domain.MqConsumerProxy;
import com.seventeen.mq.proxy.core.domain.MqInstance;
import com.seventeen.mq.proxy.core.domain.MqProducerProxy;
import com.seventeen.mq.proxy.core.mapper.MqInstanceMapper;
import com.seventeen.mq.proxy.core.service.IMqConsumerProxyService;
import com.seventeen.mq.proxy.core.service.IMqInstanceService;
import com.seventeen.mq.proxy.core.service.IMqProducerProxyService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * MQ实例 服务层处理
 *
 * @author seventeen
 */
@Service
public class MqInstanceServiceImpl implements IMqInstanceService {

    @Autowired
    private MqInstanceMapper mqInstanceMapper;
    @Autowired
    private IMqConsumerProxyService mqConsumerProxyService;
    @Autowired
    private IMqProducerProxyService mqProducerProxyService;

    @Override
    public MqInstance selectById(Long id) {
        return mqInstanceMapper.selectById(id);
    }

    @Override
    public List<MqInstance> selectList(MqInstance mqInstance) {
        return mqInstanceMapper.selectList(mqInstance);
    }

    @Override
    public int add(MqInstance mqInstance) {
        mqInstance.setDelFlag(DelFlagConstants.NORMAL);
        return mqInstanceMapper.insert(mqInstance);
    }

    private void validDelete(Long instanceId) {
        MqConsumerProxy queryConsumerProxyPram = new MqConsumerProxy();
        queryConsumerProxyPram.setInstanceId(instanceId);
        queryConsumerProxyPram.setDelFlag(DelFlagConstants.NORMAL);
        if (CollectionUtils.isNotEmpty(mqConsumerProxyService.queryList(queryConsumerProxyPram))) {
            throw new BaseException("此实例正在使用中，不可删除！");
        }

        MqProducerProxy queryProducerProxyParam = new MqProducerProxy();
        queryProducerProxyParam.setInstanceId(instanceId);
        queryProducerProxyParam.setDelFlag(DelFlagConstants.NORMAL);
        if (CollectionUtils.isNotEmpty(mqProducerProxyService.queryList(queryProducerProxyParam))) {
            throw new BaseException("此实例正在使用中，不可删除！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(MqInstance mqInstance) {
        if (DelFlagConstants.DELETED.equals(mqInstance.getDelFlag())) {
            validDelete(mqInstance.getId());
        }
        //如果实例变化，则所有消费代理和生产代理版本都会发生变化
        MqConsumerProxy queryConsumerProxyPram = new MqConsumerProxy();
        queryConsumerProxyPram.setInstanceId(mqInstance.getId());
        mqConsumerProxyService.queryList(queryConsumerProxyPram).forEach(mqConsumerProxy -> {
            MqConsumerProxy updateData = new MqConsumerProxy();
            updateData.setId(mqConsumerProxy.getId());
            mqConsumerProxyService.update(updateData);
        });
        MqProducerProxy queryProducerProxyParam = new MqProducerProxy();
        queryProducerProxyParam.setInstanceId(mqInstance.getId());
        mqProducerProxyService.queryList(queryProducerProxyParam).forEach(mqProducerProxy -> {
            MqProducerProxy updateData = new MqProducerProxy();
            updateData.setId(mqProducerProxy.getId());
            mqProducerProxyService.update(updateData);
        });
        return mqInstanceMapper.update(mqInstance);
    }


}
